package com.zq.assistant.ui.main;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.zq.assistant.R;
import com.zq.assistant.ui.BaseActivity;
import com.zq.assistant.ui.widget.recyclerview.multi.MultiRecyclerView;
import com.zq.assistant.ui.widget.recyclerview.adapter.CommonRecyclerAdapter;
import com.zq.assistant.ui.widget.recyclerview.RecyclerViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by zhangqiang on 17-6-2.
 */

public class RecyclerViewTestActivity extends BaseActivity {

    @InjectView(R.id.multi_recycler_view)
    MultiRecyclerView multiRecyclerView;
    private RecyclerView.Adapter testAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        ButterKnife.inject(this);


        testAdapter = new TestAdapter2(this);
        multiRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        multiRecyclerView.setAdapter(testAdapter);
    }

    private class TestAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {

        List<String> arrayList = new ArrayList<>();


        private Context context;

        public TestAdapter( Context context) {
            this.context = context;
            for (int i = 0; i < 10; i++) {
                arrayList.add(i+"");
            }
            setHasStableIds(true);
        }


        @Override
        public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return RecyclerViewHolder.create(context,android.R.layout.simple_list_item_1,parent);
        }

        @Override
        public void onBindViewHolder(RecyclerViewHolder holder, final int position) {

            holder.setText(android.R.id.text1,arrayList.get(position));
            holder.getView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    removeDataAtIndex(position);
                }
            });
        }

        @Override
        public int getItemCount() {
            return arrayList.size();
        }

        @Override
        public long getItemId(int position) {
            return arrayList.get(position).hashCode();
        }

        public void removeDataAtIndex(int position) {

            arrayList.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position,arrayList.size() - position);
        }
    }

    private class TestAdapter2 extends CommonRecyclerAdapter<String> {

        List<String> arrayList = new ArrayList<>();

        public TestAdapter2(Context context) {
            super(null, context, android.R.layout.simple_list_item_1);
            for (int i = 0; i < 10; i++) {
                arrayList.add(i+"");
            }
            setDataList(arrayList);
        }


        @Override
        public void onBindContentViewHolder(RecyclerViewHolder holder, final int position) {
            holder.setText(android.R.id.text1,getDataAt(position));
            holder.getView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    removeDataAtIndex(position);
                }
            });
        }

        @Override
        public void removeDataAtIndex(int position) {
            super.removeDataAtIndex(position);
        }
    }
}
